<?php
require_once 'classes/Database.php';
require_once 'classes/OrderManager.php';
require_once 'classes/SettingsHelper.php';

$orderManager = new OrderManager();
$siteName = getSiteName();

$orderNo = $_GET['out_trade_no'] ?? '';
$order = $orderManager->getOrderByNo($orderNo);

if (!$order) {
    header('Location: index.php');
    exit;
}

// 直接从订单中获取CDK码
$cdkCode = $order['cdk_code'] ?? '';
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>支付结果 - <?php echo htmlspecialchars($siteName); ?></title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css" rel="stylesheet">
    <style>
        .success-card {
            background: linear-gradient(135deg, #4facfe 0%, #00f2fe 100%);
            color: white;
            border-radius: 20px;
            padding: 40px;
            margin-bottom: 30px;
        }
        .cdk-code {
            background: rgba(255,255,255,0.2);
            border: 2px dashed rgba(255,255,255,0.5);
            border-radius: 15px;
            padding: 20px;
            font-size: 1.5rem;
            font-weight: bold;
            letter-spacing: 2px;
            text-align: center;
            margin: 20px 0;
        }
        .btn-copy {
            background: rgba(255,255,255,0.2);
            border: 1px solid rgba(255,255,255,0.5);
            color: white;
        }
        .btn-copy:hover {
            background: rgba(255,255,255,0.3);
            color: white;
        }
        .order-info {
            background: #f8f9fa;
            border-radius: 15px;
            padding: 25px;
        }
    </style>
</head>
<body>
    <!-- 导航 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <div class="container">
            <a class="navbar-brand" href="index.php">
                <i class="fas fa-shopping-cart me-2"></i><?php echo htmlspecialchars($siteName); ?>
            </a>
        </div>
    </nav>

    <div class="container mt-4">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <?php if ($order['status'] === 'completed' && $cdkCode): ?>
                    <!-- 支付成功 -->
                    <div class="success-card text-center">
                        <i class="fas fa-check-circle fa-4x mb-3"></i>
                        <h2 class="mb-3">支付成功！</h2>
                        <p class="lead mb-4">恭喜您购买成功，以下是您的CDK码</p>
                        
                        <div class="cdk-code">
                            <?php echo htmlspecialchars($cdkCode); ?>
                        </div>
                        
                        <button class="btn btn-copy" onclick="copyCDK()">
                            <i class="fas fa-copy me-2"></i>复制CDK码
                        </button>
                    </div>

                    <!-- 订单信息 -->
                    <div class="order-info">
                        <h4 class="mb-4">
                            <i class="fas fa-receipt me-2"></i>订单信息
                        </h4>
                        <div class="row">
                            <div class="col-md-6">
                                <p><strong>订单号：</strong><?php echo htmlspecialchars($order['order_no']); ?></p>
                                <p><strong>商品名称：</strong><?php echo htmlspecialchars($order['product_name']); ?></p>
                            </div>
                            <div class="col-md-6">
                                <p><strong>支付金额：</strong>¥<?php echo number_format($order['total_amount'], 2); ?></p>
                                <p><strong>支付时间：</strong><?php echo date('Y-m-d H:i:s'); ?></p>
                            </div>
                        </div>
                        <div class="row mt-3">
                            <div class="col-12">
                                <p><strong>联系方式：</strong><?php echo htmlspecialchars($order['contact_info']); ?></p>
                                <div class="alert alert-warning">
                                    <i class="fas fa-exclamation-triangle me-2"></i>
                                    <strong>重要提示：</strong>请妥善保管CDK码，遗失不补。CDK码也会发送到您的联系方式。
                                </div>
                            </div>
                        </div>
                    </div>

                    <!-- 操作按钮 -->
                    <div class="text-center mt-4">
                        <a href="index.php" class="btn btn-primary me-3">
                            <i class="fas fa-home me-2"></i>返回首页
                        </a>
                        <a href="query.php?order_no=<?php echo urlencode($order['order_no']); ?>" class="btn btn-outline-primary">
                            <i class="fas fa-search me-2"></i>查询订单
                        </a>
                    </div>

                <?php elseif ($order['status'] === 'pending'): ?>
                    <!-- 支付处理中 -->
                    <div class="card text-center">
                        <div class="card-body py-5">
                            <i class="fas fa-clock fa-4x text-warning mb-3"></i>
                            <h3 class="text-warning">支付处理中</h3>
                            <p class="lead">您的订单正在处理中，请稍后查询支付结果</p>
                            <div class="mt-4">
                                <a href="query.php?order_no=<?php echo urlencode($order['order_no']); ?>" class="btn btn-primary">
                                    <i class="fas fa-sync me-2"></i>刷新状态
                                </a>
                                <a href="index.php" class="btn btn-outline-secondary ms-2">
                                    <i class="fas fa-home me-2"></i>返回首页
                                </a>
                            </div>
                        </div>
                    </div>
                <?php else: ?>
                    <!-- 支付失败 -->
                    <div class="card text-center">
                        <div class="card-body py-5">
                            <i class="fas fa-times-circle fa-4x text-danger mb-3"></i>
                            <h3 class="text-danger">支付失败</h3>
                            <p class="lead">支付过程中出现问题，请重新尝试</p>
                            <div class="mt-4">
                                <a href="buy.php?id=<?php echo $order['product_id']; ?>" class="btn btn-primary">
                                    <i class="fas fa-redo me-2"></i>重新购买
                                </a>
                                <a href="index.php" class="btn btn-outline-secondary ms-2">
                                    <i class="fas fa-home me-2"></i>返回首页
                                </a>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        function copyCDK() {
            const cdkCode = document.querySelector('.cdk-code').textContent.trim();
            navigator.clipboard.writeText(cdkCode).then(function() {
                // 显示复制成功提示
                const btn = event.target;
                const originalText = btn.innerHTML;
                btn.innerHTML = '<i class="fas fa-check me-2"></i>已复制';
                btn.disabled = true;
                
                setTimeout(function() {
                    btn.innerHTML = originalText;
                    btn.disabled = false;
                }, 2000);
            }).catch(function(err) {
                alert('复制失败，请手动复制：' + cdkCode);
            });
        }

        // 自动检查支付状态（如果还在处理中）
        <?php if ($order['status'] === 'pending'): ?>
        setTimeout(function() {
            window.location.reload();
        }, 5000); // 5秒后自动刷新
        <?php endif; ?>
    </script>
</body>
</html>
